package controllers.games;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.Timer;

import models.games.PesciDisorientati;
import models.games.PesciDisorientati.CardinalPoints;
import views.games.PesciDisorientatiView;
import core.GameLauncher;
import core.GameLauncher.State;
import core.constants.Constants;

public class PesciDisorientatiController implements ActionListener, KeyListener {

	private PesciDisorientati model;
	private PesciDisorientatiView view;
	private GameLauncher gameLauncher;
	private int round = 0;
	private boolean started = false;

	public PesciDisorientatiController(PesciDisorientati model,
			PesciDisorientatiView view, GameLauncher gameLauncher) {
		this.model = model;
		this.view = view;
		this.gameLauncher = gameLauncher;
		view.addKeyListenerToTheGame(this);
		view.addActionListenerToTheGame(this);
	}

	@Override
	public void keyPressed(KeyEvent k) {
		if (!started) {
			if (k.getKeyCode() == KeyEvent.VK_ENTER) {
				view.update(model.getFishesDirection().ordinal(),
						model.getFishesOrientations(), model.getIsDirection());
				started = true;
			}
			else
				return;
		}
		if (round == Constants.getRounds()) {
			view.gameEnded();
			return;
		}
		boolean correctDirection = false;
		int key = k.getKeyCode();
		switch (key) {
		case KeyEvent.VK_UP:
			correctDirection = model.verify(CardinalPoints.NORTH);
			break;
		case KeyEvent.VK_DOWN:
			correctDirection = model.verify(CardinalPoints.SOUTH);
			break;
		case KeyEvent.VK_RIGHT:
			correctDirection = model.verify(CardinalPoints.EAST);
			break;
		case KeyEvent.VK_LEFT:
			correctDirection = model.verify(CardinalPoints.WEST);
			break;
		}
		model.refresh();
		if (correctDirection) {
			gameLauncher.correctAnswer();
		}
		view.setScore(model.getScore());
		view.update(model.getFishesDirection().ordinal(),
				model.getFishesOrientations(), model.getIsDirection());
		view.setGameClock(++round);
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (!started)
			return;
		boolean correctDirection = false;
		JButton b = (JButton) e.getSource();
		String inputText = b.getText();
		if (inputText.equals("Ricomincia")) {
			PesciDisorientati newModel = new PesciDisorientati(
					model.getCurrentUser());
			PesciDisorientatiView newView = new PesciDisorientatiView();
			PesciDisorientatiController newController = new PesciDisorientatiController(
					newModel, newView, gameLauncher);
			gameLauncher.setGame(newModel, newView, newController);
			gameLauncher.startTransition(State.GAME);
			model.addGameToDatabase();
			System.out.println("Ricomincia");
		}

		else if (inputText.equals("Menu Giochi")) {
			gameLauncher.startTransition(State.GAME_MENU);
			model.addGameToDatabase();
			System.out.println("Menu Giochi");
		} else if (round == Constants.getRounds()) {
			view.gameEnded();
			return;
		} else {
			switch (b.getName()) {
			case "SU":
				correctDirection = model.verify(CardinalPoints.NORTH);
				break;
			case "GIU'":
				correctDirection = model.verify(CardinalPoints.SOUTH);
				break;
			case "DESTRA":
				correctDirection = model.verify(CardinalPoints.EAST);
				break;
			case "SINISTRA":
				correctDirection = model.verify(CardinalPoints.WEST);
				break;
			}
			model.refresh();
			if (correctDirection) {
				gameLauncher.correctAnswer();
			}
			view.setScore(model.getScore());
			view.update(model.getFishesDirection().ordinal(),
					model.getFishesOrientations(), model.getIsDirection());
			view.setGameClock(++round);
		}
	}

}
